﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.HeThong.HT_ThongTinCongTy;
using csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa;
using Csla.Xaml;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiMuaSanPham;
using csla4Skeleton.UI.SL.NghiepVu.TaiSanCoDinh.DM_DoiTuongTHCP;
using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.MSP_MuaHangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

using ConfigurationNghiepVu;

namespace csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuNhapKho
{
    public partial class K_PhieuNhapKho_ChildWindow: ChildWindow
    {
        public String NguoiGiao;
        public object ChiTietModel;
        public object PhieuNhapModel = null;
        public NghiepVuType type;
        public int CountSave = 0;
        public event EventHandler OnClosedChildWindow;
        public event EventHandler LapPhieuNhapCompleteted;
        List<Control> controls = new List<Control>();
        protected K_PhieuNhapKho_ViewModel k_PhieuNhapKho_ViewModel;
        private System.Nullable<int> Khoa = null;
       
         public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public K_PhieuNhapKho_ChildWindow(object PhieuChiModel , object ChiTietModel,NghiepVuType type)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuNhapKho.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.type = type;
            this.PhieuNhapModel = PhieuChiModel;
            this.ChiTietModel = ChiTietModel;
            if (this.type.Equals(NghiepVuType.MuaHangHoa))
            {
                this.MuaHangHoaTabControl.Visibility = Visibility.Visible;
            }
            
        }

        public K_PhieuNhapKho_ChildWindow(String NguoiGiao, object ChiTietModel, NghiepVuType type)
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.Kho.K_PhieuNhapKho.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            this.NguoiGiao = NguoiGiao;
            this.type = type;
            this.ChiTietModel = ChiTietModel;
            if (this.type.Equals(NghiepVuType.MuaHangHoa))
            {
                this.MuaHangHoaTabControl.Visibility = Visibility.Visible;
            }
            
        }
         public K_PhieuNhapKho_ChildWindow()
        {
           
            InitializeComponent();
    
        }

        protected override void OnClosed(EventArgs e)
        {
            if (OnClosedChildWindow != null)
            {
                OnClosedChildWindow(this, new EventArgs());
            }
            base.OnClosed(e);

        }
         private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource k_PhieuNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuNhapKho_ViewModelSource"];
                if (this.PhieuNhapModel == null)
                {
                    k_PhieuNhapKho_ViewModel = new K_PhieuNhapKho_ViewModel(BusinessConfiguration.Configuration.ChildWindow);
                    k_PhieuNhapKho_CollectionViewSource.Source = new List<object> { k_PhieuNhapKho_ViewModel };
                    k_PhieuNhapKho_ViewModel.Saved += new EventHandler(K_PhieuNhapKho_ViewModel_Saved);
                    k_PhieuNhapKho_ViewModel.Canceled += new EventHandler(K_PhieuNhapKho_ViewModel_Canceled);
                    k_PhieuNhapKho_ViewModel.LoadCompleted += new EventHandler(K_PhieuNhapKho_ViewModel_LoadCompleted);
                    k_PhieuNhapKho_ViewModel.hangHoaIdChanged += new K_PhieuNhapKho_ViewModel.HangHoaIdChanged_EventHandler(OnHangHoaIdChanged);
                }
                else
                {
                    k_PhieuNhapKho_ViewModel = this.PhieuNhapModel as K_PhieuNhapKho_ViewModel;
                    k_PhieuNhapKho_CollectionViewSource.Source = new List<object> { k_PhieuNhapKho_ViewModel };
                    k_PhieuNhapKho_ViewModel.Saved += new EventHandler(K_PhieuNhapKho_ViewModel_Saved);
                    k_PhieuNhapKho_ViewModel.Canceled += new EventHandler(K_PhieuNhapKho_ViewModel_Canceled);
                    k_PhieuNhapKho_ViewModel.LoadCompleted += new EventHandler(K_PhieuNhapKho_ViewModel_LoadCompleted);
                    k_PhieuNhapKho_ViewModel.hangHoaIdChanged += new K_PhieuNhapKho_ViewModel.HangHoaIdChanged_EventHandler(OnHangHoaIdChanged);

                    if (this.type.Equals(NghiepVuType.MuaHangHoa))
                    {
                        System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                        MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = this.ChiTietModel as MSP_MuaHangHoa_ViewModel;
                        muaHangHoaViewModel.ChildList_AddedNew += new MSP_MuaHangHoa_ViewModel.ChildList_AddedNew_EventHandler(msp_MuaHangHoa_ViewModel_ChildList_AddedNew);
                        muaHangHoaViewModel.hangHoaIdChanged += new MSP_MuaHangHoa_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaHangHoa_HangHoaIdChanged);
                        muaHangHoaViewModel.ThueSuatIdChanged += new MSP_MuaHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaHangHoa_ThueSuatIdChanged);
                        muaHangHoaViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                        msp_MuaHangHoa_CollectionViewSource.Source = new List<object> { muaHangHoaViewModel };
                        DoLoadForMuaHangHoa();
                    }
                }



                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource ht_ThongTinCongTy_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
                HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = new HT_ThongTinCongTy_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                ht_ThongTinCongTy_CollectionViewSource.Source = new List<object> { ht_ThongTinCongTy_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_DoiTuongTHCP_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DoiTuongTHCP_ListViewModelSource"];
                DM_DoiTuongTHCP_ListViewModel dm_DoiTuongTHCP_ListViewModel = new DM_DoiTuongTHCP_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DoiTuongTHCP_CollectionViewSource.Source = new List<object> { dm_DoiTuongTHCP_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_CollectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiMuaSanPham_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiMuaSanPham_ListViewModelSource"];
                DM_LoaiMuaSanPham_ListViewModel dm_LoaiMuaSanPham_ListViewModel = new DM_LoaiMuaSanPham_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiMuaSanPham_CollectionViewSource.Source = new List<object> { dm_LoaiMuaSanPham_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };
                
                
                             
            }
        }

         private void OnTongTienHangChanged(object sender, EventArgs e)
         {
             if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
             {
                 this.GetViewModel().Model.TongTien = this.GetMuaHangHoaViewModel().Model.TongTienHang;
                 this.GetViewModel().Model.TongTienQĐ = this.GetMuaHangHoaViewModel().Model.TongTienHang * this.GetMuaHangHoaViewModel().Model.TyGia;
             }
             
         }
        private void OnHangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            this.GetViewModel().DoChangingBusy(false);
        }
        private K_PhieuNhapKho_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource K_PhieuNhapKho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["K_PhieuNhapKho_ViewModelSource"];
            return ((List<object>)K_PhieuNhapKho_CollectionViewSource.Source)[0] as K_PhieuNhapKho_ViewModel;
        }

        void K_PhieuNhapKho_ViewModel_Canceled(object sender, EventArgs e)
        {
            this.DialogResult = false;
        }

     

        private void DoLoadForMuaHangHoa()
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            this.GetViewModel().Model.TongTien = muaHangHoaViewModel.Model.TongTienHang;
            this.GetViewModel().Model.TongTienQĐ = muaHangHoaViewModel.Model.TongTienHangQĐ;

            if (muaHangHoaViewModel.Model.LoaiMuaSanPhamId == 1)
            {
                this.MSP_MuaHangHoa_ThueNKTab.Visibility = Visibility.Collapsed;
                this.MSP_MuaHangHoa_ThueTTDBTab.Visibility = Visibility.Collapsed;
            }
            if (muaHangHoaViewModel.Model.LoaiTienId == 3)
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[8].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[10].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[13].IsVisible = false;
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[15].IsVisible = false;
                this.MSP_MuaHangHoa_ThueNKGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueTTDBGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                this.MSP_MuaHangHoa_TSThieuCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_TSThuaCXLGridView.Columns[3].IsVisible = false;
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.Columns[3].IsVisible = false;
            }

            if (this.GetMuaHangHoaViewModel().CanSave == true)
            {
                GetMuaHangHoaViewModel().Save(this, new Csla.Xaml.ExecuteEventArgs());
            }
        }
        void K_PhieuNhapKho_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            if(this.type == NghiepVuType.MuaHangHoa)
            {
                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = this.ChiTietModel as MSP_MuaHangHoa_ViewModel;
                muaHangHoaViewModel.ChildList_AddedNew += new MSP_MuaHangHoa_ViewModel.ChildList_AddedNew_EventHandler(msp_MuaHangHoa_ViewModel_ChildList_AddedNew);
                muaHangHoaViewModel.hangHoaIdChanged += new MSP_MuaHangHoa_ViewModel.HangHoaIdChanged_EventHandler(msp_MuaHangHoa_HangHoaIdChanged);
                muaHangHoaViewModel.ThueSuatIdChanged += new MSP_MuaHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(msp_MuaHangHoa_ThueSuatIdChanged);
                muaHangHoaViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                msp_MuaHangHoa_CollectionViewSource.Source = new List<object> { muaHangHoaViewModel };

                K_PhieuNhapKho_ViewModel phieunhapViewModel = this.GetViewModel();
                phieunhapViewModel.Model.NhaCungCapId = muaHangHoaViewModel.Model.NhaCungCapId;
                phieunhapViewModel.Model.NguoiGiao = this.NguoiGiao;
                phieunhapViewModel.Model.DienGiai = "Nhập kho từ mua hàng hóa";
                phieunhapViewModel.Model.LoaiChungTuThayTheId = muaHangHoaViewModel.Model.LoaiChungTuId;
                phieunhapViewModel.Model.ChungTuThayTheId = muaHangHoaViewModel.Model.Khoa;

                phieunhapViewModel.Model.TongTien = muaHangHoaViewModel.Model.TongTienHang;
                phieunhapViewModel.Model.TongTienQĐ = muaHangHoaViewModel.Model.TongTienHangQĐ;

                if (muaHangHoaViewModel.Model.LoaiMuaSanPhamId == 1)
                {
                    this.MSP_MuaHangHoa_ThueNKTab.Visibility = Visibility.Collapsed;
                    this.MSP_MuaHangHoa_ThueTTDBTab.Visibility = Visibility.Collapsed;
                }
                if (muaHangHoaViewModel.Model.LoaiTienId == 3)
                {
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[8].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[10].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[13].IsVisible = false;
                    this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.Columns[15].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueNKGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueTTDBGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                    this.MSP_MuaHangHoa_TSThieuCXLGridView.Columns[3].IsVisible = false;
                    this.MSP_MuaHangHoa_TSThuaCXLGridView.Columns[3].IsVisible = false;
                    this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.Columns[3].IsVisible = false;
                }

                phieunhapViewModel.Save(this, new Csla.Xaml.ExecuteEventArgs());
              
                
            }
            
        }
        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                K_PhieuNhapKho_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                


            }
            e.Handled = true;
        }

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();

        }



        public void AddItems()
        {
             if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    AddMuaHangHoaDetailsItem();
                }
            }

        }
        private RadGridView GetActiveGridView()
        {
            if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_MuaHangHoaChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueChiTietTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueChiTietGridView;
            }
            else if (this.MSP_MuaHangHoa_ChiPhiMuaHangTab.IsSelected)
            {
                return MSP_MuaHangHoa_ChiPhiMuaHangGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThieuCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThieuCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_TSThuaCXLTab.IsSelected)
            {
                return MSP_MuaHangHoa_TSThuaCXLGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueNKTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueNKGridView;
            }
            else if (this.MSP_MuaHangHoa_ThueTTDBTab.IsSelected)
            {
                return MSP_MuaHangHoa_ThueTTDBGridView;
            }


            return null;
        }



        void DeleteItems()
        {
           if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.MSP_MuaHangHoa_MuaHangHoaChiTietTab.IsSelected)
                {
                    MSP_MuaHangHoa_ChiTietChild ChiTietChild = (MSP_MuaHangHoa_ChiTietChild)(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.SelectedItem);
                    IList<MSP_MuaHangHoa_ChiTietChild> list = this.GetMuaHangHoaViewModel().Model.MSP_MuaHangHoa_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteMuaHangHoaDetailsItem(index);
                }
            }
        }

        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
           if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetMuaHangHoaViewModel().SaveAndNew();
            }

        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaHangHoaDetailsItem();
                    }

                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaHangHoaDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                     if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddMuaHangHoaDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {

            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.MuaHangHoaTabControl.SelectedItem = MuaHangHoaTabControl.Items[0];
                this.AddMuaHangHoaDetailsItem();
            }

        }


        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {
            if (MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.MuaHangHoaTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == MuaHangHoaTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView, e.Index);

                }
                else
                {
                    this.MuaHangHoaTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ChiPhiMuaHangTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ChiPhiMuaHangGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueChiTietGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueNKTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueNKGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_ThueTTDBTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_ThueTTDBGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThieuCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThieuCXLGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("MSP_MuaHangHoa_TSThuaCXLTab"))
                    {
                        FocusAfterChangeTab(this.MSP_MuaHangHoa_TSThuaCXLGridView, e.Index);

                    }
                }
            }
        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {

            Dispatcher.BeginInvoke(new Action(() =>
            {
                grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                grid.Focus();
                grid.BeginEdit();
            }));


        }
        private MSP_MuaHangHoa_ViewModel GetMuaHangHoaViewModel()
        {
            System.Windows.Data.CollectionViewSource MSP_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            return ((List<object>)MSP_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;
        }

        void msp_MuaHangHoa_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                    break;
                }
            }
        }

         void msp_MuaHangHoa_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
            MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

            MSP_MuaHangHoa_ChiTietChild ChiTietChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiTiets[e.Index];
            MSP_MuaHangHoa_ThueChild ThueChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_Thues[e.Index];
            MSP_MuaHangHoa_ChiPhiMuaHangChild ChiPhiMuaHangChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ChiPhiMuaHangs[e.Index];
            MSP_MuaHangHoa_TSThuaCXLChild TSThuaCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThuaCXLs[e.Index];
            MSP_MuaHangHoa_TSThieuCXLChild TSThieuCXLChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_TSThieuCXLs[e.Index];
            MSP_MuaHangHoa_ThueNKChild ThueNKChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueNKs[e.Index];
            MSP_MuaHangHoa_ThueTTDBChild ThueTTDBChild = muaHangHoaViewModel.Model.MSP_MuaHangHoa_ThueTTDBs[e.Index];

            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = ((List<object>)dm_TaiKhoan_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTaiKhoan = dm_TaiKhoan_ListViewModel.Model.getListOfInfo();


            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;
                    ChiTietChild.TKKhoId = (int)item.TKKhoId;
                    ChiTietChild.DonGia = item.DonGiaBan;
                    ChiTietChild.TKNoId = (int)item.TKKhoId;
                    ChiTietChild.TKCoId = listTaiKhoan[0].Khoa;
                    ChiTietChild.DVTId = item.DonViTinhId;

                    ThueChild.HangHoaId = item.Khoa;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.LoaiHoaDonId = muaHangHoaViewModel.Model.LoaiHoaDonId;
                    ThueChild.ThueSuatId = item.ThueSuatId;
                    ThueChild.TKNoId = listTaiKhoan[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;


                    ThueNKChild.HangHoaId = item.Khoa;
                    ThueNKChild.DienGiai = item.TenViet;
                    ThueNKChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueNKChild.TkCoId = listTaiKhoan[0].Khoa;



                    TSThuaCXLChild.HangHoaId = item.Khoa;
                    TSThuaCXLChild.DienGiai = item.TenViet;
                    TSThuaCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThuaCXLChild.TkCoId = listTaiKhoan[0].Khoa;

                    TSThieuCXLChild.HangHoaId = item.Khoa;
                    TSThieuCXLChild.DienGiai = item.TenViet;
                    TSThieuCXLChild.TkNoId = listTaiKhoan[0].Khoa;
                    TSThieuCXLChild.TkCoId = listTaiKhoan[0].Khoa;


                    ChiPhiMuaHangChild.HangHoaId = item.Khoa;
                    ChiPhiMuaHangChild.DienGiai = item.TenViet;
                    ChiPhiMuaHangChild.TkNoId = listTaiKhoan[0].Khoa;
                    ChiPhiMuaHangChild.TkCoId = listTaiKhoan[0].Khoa;

                    ThueTTDBChild.HangHoaId = item.Khoa;
                    ThueTTDBChild.DienGiai = item.TenViet;
                    ThueTTDBChild.TkNoId = listTaiKhoan[0].Khoa;
                    ThueTTDBChild.TkCoId = listTaiKhoan[0].Khoa;

                    break;
                }
            }
        }
        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_NhaCungCap);
        }

       
        

        void K_PhieuNhapKho_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            K_PhieuNhapKho_ChiTietChild ChiTietChild =  GetViewModel().Model.K_PhieuNhapKho_ChiTiets[e.Index];
           
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.DonGia = (double) item.DonGiaBan;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;
                    ChiTietChild.DVTId = item.DonViTinhId;
                       
                    break;
                }
            }
            
        }
     
       

        private void DeletePhieuNhapKhoDetailsItem(int Index)
        {
            GetViewModel().DeleteK_PhieuNhapKho_ChiTietItem( Index);
        }


        private void AddMuaHangHoaDetailsItem()
        {
            GetMuaHangHoaViewModel().AddMSP_MuaHangHoa_ChiTietItem();
        }


        private void DeleteMuaHangHoaDetailsItem(int Index)
        {
            GetMuaHangHoaViewModel().DeleteMSP_MuaHangHoa_ChiTietItem(Index);
        }

       

        void msp_MuaHangHoa_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "MSP_MuaHangHoa_ChiTiets")
            {
                this.MSP_MuaHangHoa_MuaHangHoaChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_Thues")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ChiPhiMuaHangs")
            {
                this.MSP_MuaHangHoa_ChiPhiMuaHangGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThieuCXLs")
            {
                this.MSP_MuaHangHoa_TSThieuCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_TSThuaCXLs")
            {
                this.MSP_MuaHangHoa_TSThuaCXLGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueNKs")
            {
                this.MSP_MuaHangHoa_ThueNKGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "MSP_MuaHangHoa_ThueTTDBs")
            {
                this.MSP_MuaHangHoa_ThueTTDBGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }




        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.MuaHangHoaTabControl.Visibility == Visibility.Visible)
            {

                System.Windows.Data.CollectionViewSource msp_MuaHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["MSP_MuaHangHoa_ViewModelSource"];
                MSP_MuaHangHoa_ViewModel muaHangHoaViewModel = ((List<object>)msp_MuaHangHoa_CollectionViewSource.Source)[0] as MSP_MuaHangHoa_ViewModel;

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuNhapKho();
                }
                if (muaHangHoaViewModel.CanSave)
                {
                    muaHangHoaViewModel.SaveMuaHangHoa();
                }

            }
           
        }

        int IndexOfRow = 0;
        private void K_PhieuNhapKho_ViewModel_Saved(object sender, EventArgs e)
        {
           
           
            if (LapPhieuNhapCompleteted != null)
            {
                LapPhieuNhapCompleteted(this, new EventArgs());
            }

        }
       

    }
}


